<?php

namespace App\Livewire\Admin\User;

use App\Models\User;
use Livewire\Attributes\Layout;
use Livewire\Component;

#[Layout('components.admin.layout')]
class Create extends Component
{
    public $name;
    public $email;
    public $password;

    public function store()
    {
        $validated = $this->validate([
            'name' => 'required',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:6',
        ]);
        $user = new User();
        $user->name = $this->name;
        $user->email = $this->email;
        $user->password = bcrypt($this->password);
        $res = $user->save();
        // TODO 添加成功后要在页面加入该条数据
        if ($res) {
            $html = view('admin.user.list-item', ['user' => $user])->render();
            $this->dispatch('append-row', ['html' => $html]);
            $this->dispatch('close-modal');
        }
    }

    public function render()
    {
        return view('livewire.admin.user.create');
    }
}
